//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "VarianceSwap.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instrument.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CVarianceSwap::CVarianceSwap (QL::Position::TypeEnum position, Double strike, Double notional, DateTime startDate, DateTime maturityDate, Cephei::QL::IPricingEngine^ QL_Pricer) : CInstrument(CVarianceSwap::typeid)
{
    try
    {
#ifdef HANDLE
        _phVarianceSwap = NULL;
#endif
        QuantLib::Position::Type _position = (QuantLib::Position::Type)position ;
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //d
        QuantLib::Real _notional = (QuantLib::Real)ValueHelper::Convert (notional); //d
        QuantLib::Date _startDate = (QuantLib::Date)ValueHelper::Convert (startDate); //d
        QuantLib::Date _maturityDate = (QuantLib::Date)ValueHelper::Convert (maturityDate); //d
        _ppVarianceSwap = new boost::shared_ptr<QuantLib::VarianceSwap> (new QuantLib::VarianceSwap ( _position,  _strike,  _notional,  _startDate,  _maturityDate ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppVarianceSwap)->setPricingEngine (_QL_Pricer);
        SetInstrument (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppVarianceSwap));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Instruments::CVarianceSwap::CVarianceSwap (boost::shared_ptr<QuantLib::VarianceSwap>& childNative, Object^ owner) : CInstrument(CVarianceSwap::typeid)
{
#ifdef HANDLE
	_phVarianceSwap = NULL;
#endif
	_ppVarianceSwap = &childNative;
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppVarianceSwap));
}
Cephei::QL::Instruments::CVarianceSwap::CVarianceSwap (QuantLib::VarianceSwap& childNative, Object^ owner) : CInstrument(CVarianceSwap::typeid)
{
#ifdef HANDLE
	_phVarianceSwap = NULL;
#endif
	_ppVarianceSwap = new boost::shared_ptr<QuantLib::VarianceSwap> (&childNative);
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppVarianceSwap));
    _VarianceSwapOwner = owner;
    _InstrumentOwner = owner;
}

Cephei::QL::Instruments::CVarianceSwap::CVarianceSwap (CVarianceSwap^ copy) : CInstrument(CVarianceSwap::typeid)
{
#ifdef HANDLE
	_phVarianceSwap = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppVarianceSwap = new boost::shared_ptr<QuantLib::VarianceSwap> (copy->GetShared());
        _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppVarianceSwap));
    }
}
Cephei::QL::Instruments::CVarianceSwap::CVarianceSwap (PLATFORM::Type^ t) : CInstrument(CVarianceSwap::typeid)
{
#ifdef HANDLE
	_phVarianceSwap = NULL;
#endif
	if (!t->IsSubclassOf(CVarianceSwap::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CVarianceSwap::CVarianceSwap (QuantLib::Handle<QuantLib::VarianceSwap>& childNative, Object^ owner)  : CInstrument(CVarianceSwap::typeid)
{
	_phVarianceSwap = &childNative;
	_ppVarianceSwap = &static_cast<boost::shared_ptr<QuantLib::VarianceSwap>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppVarianceSwap));
    _VarianceSwapOwner = owner;
}
Cephei::QL::Instruments::CVarianceSwap::CVarianceSwap (QuantLib::Handle<QuantLib::VarianceSwap> childNative)  : CInstrument(CVarianceSwap::typeid)
{
	_phVarianceSwap = &childNative;
	_ppVarianceSwap = &static_cast<boost::shared_ptr<QuantLib::VarianceSwap>>(childNative.currentLink());
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppVarianceSwap));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CVarianceSwap::CVarianceSwap (QuantLib::VarianceSwap childNative)  : CInstrument(CVarianceSwap::typeid)
{
#ifdef HANDLE
	_phVarianceSwap = NULL;
#endif
	_ppVarianceSwap = new boost::shared_ptr<QuantLib::VarianceSwap> (new QuantLib::VarianceSwap (childNative));
    _ppInstrument = new boost::shared_ptr<QuantLib::Instrument> (boost::dynamic_pointer_cast<QuantLib::Instrument> (*_ppVarianceSwap));
}
#endif

Cephei::QL::Instruments::CVarianceSwap::~CVarianceSwap ()
{
    if (_ppVarianceSwap != NULL)
    {
	    delete _ppVarianceSwap;
        _ppVarianceSwap = NULL;
    }
}
Cephei::QL::Instruments::CVarianceSwap::!CVarianceSwap ()
{
    if (_ppVarianceSwap != NULL)
    {
	    delete _ppVarianceSwap;
    }
}
QuantLib::VarianceSwap& Cephei::QL::Instruments::CVarianceSwap::GetReference ()
{
    if (_ppVarianceSwap == NULL) throw REFNEW NativeNullException ();
	return **_ppVarianceSwap;
}
boost::shared_ptr<QuantLib::VarianceSwap>& Cephei::QL::Instruments::CVarianceSwap::GetShared ()
{
    if (_ppVarianceSwap == NULL) throw REFNEW NativeNullException ();
	return *_ppVarianceSwap;
}
QuantLib::VarianceSwap* Cephei::QL::Instruments::CVarianceSwap::GetPointer ()
{
    if (_ppVarianceSwap == NULL) throw REFNEW NativeNullException ();
	return &**_ppVarianceSwap;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::VarianceSwap>& Cephei::QL::Instruments::CVarianceSwap::GetHandle ()
{
	if (_phVarianceSwap == NULL)
	{
		_phVarianceSwap = new Handle<QuantLib::VarianceSwap> (*_ppVarianceSwap);
	}
	return *_phVarianceSwap;
}
#endif
bool Cephei::QL::Instruments::CVarianceSwap::HasNative () 
{
	return (_ppVarianceSwap != NULL);
}

Boolean Cephei::QL::Instruments::CVarianceSwap::IsExpired::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppVarianceSwap)->isExpired ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Instruments::CVarianceSwap::MaturityDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppVarianceSwap)->maturityDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CVarianceSwap::Notional::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppVarianceSwap)->notional ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Position::TypeEnum Cephei::QL::Instruments::CVarianceSwap::Position::get ()
{
    try
    {
    	QuantLib::Position::Type _rv = (QuantLib::Position::Type)(*_ppVarianceSwap)->position ( );   
        QL::Position::TypeEnum _nrv = (QL::Position::TypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Instruments::CVarianceSwap::StartDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppVarianceSwap)->startDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CVarianceSwap::Strike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppVarianceSwap)->strike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Instruments::CVarianceSwap::Variance::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppVarianceSwap)->variance ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::IVarianceSwap^ Cephei::QL::Instruments::CVarianceSwap_Factory::Create (QL::Position::TypeEnum position, Double strike, Double notional, DateTime startDate, DateTime maturityDate, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CVarianceSwap ( position,  strike,  notional,  startDate,  maturityDate,  QL_Pricer);
}
